import { login, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { getUserProfile } from "@/api/system/user";
const user = {
  state: {
    token: getToken(),
    all_count:0,//消息总通知
    name: '',
    avatar: '',
    roles: [],
    permissions: [],
    infoUser:{
      id:'',
      pageNum: '',
      pageSize:'',
      insureNo: null,
      name1: null,
      phone1: null,
      commAddrAndPostcode1: null,
      email1: null,
      name2: null,
      phone2: null,
      commAddrAndPostcode: null,
      email2: null,
      nature2: null,
      industry: null,
      address2: null,
      businessScope2: null,
      staffCount2: null,
      seniorDescription: null,
      generalDescription: null,
      isELI: null,
      isLossEli: null,
      eliDesc: null,
      isII: null,
      isLossIi: null,
      iiDesc: null,
      isTrain: null,
      trainingTime: null,
      isHaveMedicalStaff: null,
      medicalStaffCount: null,
      hospitalDistance: null,
      hospitalName: null,
      isJoinMi: null,
      descriptionMi: null,
      compPrincipleAndLimit: null,
      otherDescription: null,
      casualtiesFeeLimit: null,
      medicalFeeLimit: null,
      legalFeeLimit: null,
      liabilityLimit: null,
      deductible: null,
      insuranceRate: null,
      insuranceTimeStart: null,
      insuranceTimeEnd: null,
      insuranceFeeCount: null,
      payTime: null,
      solution: null,
      seal: null,
      signingTime: null,
      agent: null,
      underwriting: null,
      signPolicyholder: null,
      signPolicyholderTime: null,
      insUserIds: null,
      userPath: null,
      status: null
    },
   // 保单用户详情
   insureUser:{
     insureId: null, //投保id
     id: null,
     name: null,//雇主姓名
     sex: null,//性别
     idCardTypeId: null,//证件类型
     idCardTypeName: null,//证件类型名称
     idCardNo: null,//证件号码
     industryId: null,//工种id(1:1-3类；2：4类)
     industryName: null,//健康情况
     healthCond: null,//健康情况
     incomeMonth: null,//月收入
     company: null,//用工单位
     position: null,//职务名称
     remark: null,//备注
     price: null,//保费
     createTime: null,//
     updateTime: null,
     status: "0",
   },

  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    all_count:(state,data)=>{
      state.all_count=data
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
    },
    Info_Insure:(state,parms)=>{
      state.infoUser=parms;
      console.log('parms',parms);
    },
    Insure_User:(state,parms)=>{
      state.insureUser=parms;
    },

  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      const password = userInfo.password
      const code = userInfo.code
      const uuid = userInfo.uuid
      return new Promise((resolve, reject) => {
        login(username, password, code,uuid).then(res => {
          // console.log('登陆',res)
          // setToken(res.data.user.token)
          // commit('SET_TOKEN', res.data.user.token)
          setToken(res.token)
          commit('SET_TOKEN', res.token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo().then(res => {
          const user = res.user
          const avatar = user.avatar == "" ? require("@/assets/images/profile.jpg") : process.env.VUE_APP_BASE_API + user.avatar;
          if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组
            commit('SET_ROLES', res.roles)
            commit('SET_PERMISSIONS', res.permissions)
          } else {
            commit('SET_ROLES', ['ROLE_DEFAULT'])
          }
          commit('SET_NAME', user.userName)
          commit('SET_AVATAR', avatar)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 退出系统
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_PERMISSIONS', [])
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        // removeToken()
        resolve()
      })
    }
  }
}

export default user
